package cn.iocoder.yudao.module.bpm.framework.flowable.core.magicApiConfig;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.bpm.framework.flowable.core.magicApiConfig.dto.MagicApiConfigDto;
import cn.iocoder.yudao.framework.magic.api.MagicApiExcuse;
import cn.iocoder.yudao.framework.magic.api.dto.ExcuseMagicApiDTO;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.BaseElement;
import org.flowable.bpmn.model.ExtensionElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.ssssssss.script.runtime.ExitValue;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BpmCallMagicApiServiceImpl implements BpmCallMagicApiService{

    private final static String MAGIC_API_CONFIG = "magicApiConfig";

    @Autowired
    @Lazy
    MagicApiExcuse magicApiExcuse ;

    /**
     * 初始化 MagicApi 配置
     *
     * @param source
     */
    @Override
    public List<MagicApiConfigDto> initMagicApiConfig(BaseElement source) {

        List<ExtensionElement> magicApiConfig = source.getExtensionElements().get(MAGIC_API_CONFIG);
        if(CollUtil.isEmpty(magicApiConfig)){
            return Collections.emptyList();
        }
        List<MagicApiConfigDto> magicApiConfigDtoList = magicApiConfig.stream().map(
                extensionElement -> {
            MagicApiConfigDto magicApiConfigDto = new MagicApiConfigDto();

            String event = extensionElement.getAttributeValue(null, "event");
            String magicApiId = extensionElement.getAttributeValue(null, "magicApiId");
            String fullPath = extensionElement.getAttributeValue(null, "fullPath");
            String fullDir = extensionElement.getAttributeValue(null, "fullDir");
            String method = extensionElement.getAttributeValue(null, "method");

            magicApiConfigDto
                    .setEvent(event)
                    .setMagicApiId(magicApiId)
                    .setFullPath(fullPath)
                    .setFullDir(fullDir)
                    .setMethod(method);
            List<ExtensionElement> field = extensionElement.getChildElements().get("field");
            if(CollUtil.isEmpty(field)){
                magicApiConfigDto.setFields(new HashMap<>(6));
            }else {
                HashMap<String, Object> fieldMap = new HashMap<>();
                field.stream().forEach(fieldElement -> {
                    fieldMap.put(fieldElement.getAttributeValue(null, "name"),fieldElement.getAttributeValue(null, "stringValue"));
                });
                magicApiConfigDto.setFields(fieldMap);
            }

            return magicApiConfigDto;
        }).collect(Collectors.toList());

        return magicApiConfigDtoList;
    }

    /**
     * 调用 MagicApi
     *
     * @param magicApiConfigDto
     */
    @Override
    public void callMagicApiByConfig(MagicApiConfigDto magicApiConfigDto) {

        ExcuseMagicApiDTO excuseMagicApiDTO = new ExcuseMagicApiDTO();
        excuseMagicApiDTO.setMethod(magicApiConfigDto.getMethod());
        excuseMagicApiDTO.setPath(magicApiConfigDto.getFullPath());
        excuseMagicApiDTO.setContext(magicApiConfigDto.getFields());

        magicApiExcuse.excuseMagicApi(excuseMagicApiDTO);

    }
}
